package com.bilgeadam.conversationmanagement.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Bora SAYINER
 * @since Dec 28, 2014 5:30:16 PM
 * @version 1.0
 */
public class URLReWritingServlet extends HttpServlet {

	private final List<String> istanbul;
	private final List<String> ankara;

	public URLReWritingServlet() {
		this.istanbul = new ArrayList<String>();
		this.ankara = new ArrayList<String>();
	}

	@Override
	public void init() throws ServletException {
		istanbul.add("Topkapı Sarayı");
		istanbul.add("Ayasofya");
		istanbul.add("Dolmabahçe");
		istanbul.add("Kız Kulesi");
		istanbul.add("Selimiye");
		istanbul.add("Eminönü");
		ankara.add("Anıtkabir");
		ankara.add("Atakule");
		ankara.add("Ulus");
		ankara.add("Gölbaşı");
		ankara.add("Elmadağ");
		ankara.add("Keçiören");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String selectedCity = req.getParameter("city");
		if (selectedCity == null) {
			printSelectCityPage(resp);
		} else {
			printPoiPage(req, resp, selectedCity);
		}
	}

	private void printPoiPage(HttpServletRequest req, HttpServletResponse resp, String selectedCity) throws IOException {
		resp.setContentType("text/html");
		resp.setCharacterEncoding("utf-8");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<!DOCTYPE html>");
		stringBuffer.append("<html>");
		{
			stringBuffer.append("<head>");
			{
				stringBuffer.append("<meta charset='utf-8'>");
				stringBuffer.append("<title>Select City</title>");
			}
			stringBuffer.append("</head>");
			stringBuffer.append("<body>");
			{
				List<String> city = selectedCity.equals("istanbul") ? istanbul : ankara;
				int selectedPage;
				try {
					selectedPage = Integer.valueOf(req.getParameter("page"));
				} catch (Exception e) {
					selectedPage = 1;
				}
				int start = (selectedPage * 2) - 2;
				for (int i = start; i < start + 2; i++) {
					stringBuffer.append(String.format("<h3>%s</h3>", city.get(i)));
				}
				for (int i = 1; i <= istanbul.size() / 2; i++) {
					stringBuffer.append(String.format("<a href='?city=%s&page=%d'>Page %d</a> ", selectedCity, i, i));
				}

			}
			stringBuffer.append("</body>");
		}
		stringBuffer.append("</html>");
		resp.getWriter().print(stringBuffer.toString());
	}

	private void printSelectCityPage(HttpServletResponse resp) throws IOException {
		resp.setContentType("text/html");
		resp.setCharacterEncoding("utf-8");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<!DOCTYPE html>");
		stringBuffer.append("<html>");
		{
			stringBuffer.append("<head>");
			{
				stringBuffer.append("<meta charset='utf-8'>");
				stringBuffer.append("<title>Select City</title>");
			}
			stringBuffer.append("</head>");
			stringBuffer.append("<body>");
			{
				stringBuffer.append("<h1>Lütfen Şehir Seçiniz</h1>");
				stringBuffer.append("<a href='?city=istanbul'>Istanbul</a> ");
				stringBuffer.append("<a href='?city=ankara'>Ankara</a>");
			}
			stringBuffer.append("</body>");
		}
		stringBuffer.append("</html>");
		resp.getWriter().print(stringBuffer.toString());
	}

}
